package com.nw.controller;

import com.alibaba.fastjson.JSONObject;
import com.nw.common.BaseController;
import com.nw.common.Result;
import com.nw.enums.ReturnStatusEnum;
import com.nw.enums.TableName;
import com.nw.enums.ValidateReturnCodeEnum;
import com.nw.service.MongoService;
import com.nw.utils.MD5Util;
import com.nw.utils.StringUtil;
import com.nw.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@CrossOrigin(origins = "https://domain2.com,maxAge = 3600",methods = {RequestMethod.GET, RequestMethod.POST})
//@CrossOrigin(origins = "http://110.16.71.27:8080",maxAge = 3600,methods = {RequestMethod.GET})
@Controller
@RequestMapping(value = "mongoJson")
@Slf4j
public class MongoJsonController extends BaseController {
    @Autowired
    MongoService mongoService;

    /**
     *
     * @param {}
     * @return
     * @throws Exception
     */
    @RequestMapping("validate")
    @ResponseBody
    private Result validate(@RequestBody JSONObject map) {
        Result result = mongoService.validateParam(map);
        return result;
    }
    @RequestMapping("login")
    @ResponseBody
    private Result login(@RequestBody JSONObject map)  {
        Result result = new Result();
        if(StringUtil.isEmpty(map.get("pass_word")) || StringUtil.isEmpty(map.get("user_name"))){
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_NAME_PASS_EXIT_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_NAME_PASS_EXIT_FAILURE.getDesc());
            return result;
        }
        result = mongoService.login(map);
        return result;
    }
    @RequestMapping("loginOut")
    @ResponseBody
    private Result loginOut(@RequestBody JSONObject map)  {
        Result result = new Result();
          /*token登陆验证*/
        if(!mongoService.validateToken(map.get("token"))){
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getDesc());
            return  result;
        }
        if(mongoService.loginOut(map.get("token").toString()) > 0){
            result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
            result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_LOGIN_OUT_SUCCESS.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_LOGIN_OUT_SUCCESS.getDesc());
        }else{
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getDesc());
        }
        return result;
    }
    @RequestMapping("add")
    @ResponseBody
    private Result add(@RequestBody JSONObject map)  {
        Result result = new Result();
        if(StringUtil.isEmpty(map.get("pass_word")) || StringUtil.isEmpty(map.get("user_name"))){
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_NAME_PASS_EXIT_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_NAME_PASS_EXIT_FAILURE.getDesc());
            return result;
        }
        String user_name = map.get("user_name").toString();
        if(!mongoService.validateUserNameForRepeat(user_name)){
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_EXIT_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_EXIT_FAILURE.getDesc());
            return result;
        }
        String pass_word = map.get("pass_word").toString();
        String pass = MD5Util.getMD5(pass_word);
        map.put("pass_word",pass);
        map.put("_id",UUIDUtil.uuid());
        mongoService.add(TableName.TEST_USER,map);
        result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
        result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
        result.setReturnCode(ValidateReturnCodeEnum.USER_SAVE_SUCCESS.getCode());
        result.setReturnMsg(ValidateReturnCodeEnum.USER_SAVE_SUCCESS.getDesc());
        return result;
    }
    @RequestMapping("update")
    @ResponseBody
    private Result update(@RequestBody JSONObject map)  {
        Result result = mongoService.update(map);
        return result;
    }
    @RequestMapping("query")
    @ResponseBody
    private Result query(@RequestBody JSONObject map)  {
        Result result = new Result();
         /*token登陆验证*/
        if(!mongoService.validateToken(map.get("token"))){
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getDesc());
            return  result;
        }
        Integer skip = map.get("skip") !=null ? Integer.valueOf(map.get("skip").toString()):0;
        Integer limit = map.get("limit") !=null ? Integer.valueOf(map.get("limit").toString()):0;
        /*查询操作*/
        long startTime = System.currentTimeMillis();
        List<Document> list = mongoService.query(TableName.TEST_USER,map,null,skip != null ? skip:0 , limit != null ? limit:0);
        log.info("mongoJson/query耗时 : " + (System.currentTimeMillis() - startTime+"ms"));
        result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
        result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
        result.setData(list);
        return result ;
    }
    @RequestMapping("queryOne")
    @ResponseBody
    private Result queryOne(@RequestBody JSONObject map)  {
        Result result = new Result();
         /*token登陆验证*/
        if(!mongoService.validateToken(map.get("token"))){
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getDesc());
            return  result;
        }
        Integer skip = map.get("skip") !=null ? Integer.valueOf(map.get("skip").toString()):0;
        Integer limit = map.get("limit") !=null ? Integer.valueOf(map.get("limit").toString()):0;
        /*查询操作*/
        long startTime = System.currentTimeMillis();
        Document user = mongoService.queryOne(TableName.TEST_USER,map);
        log.info("mongoJson/queryOne耗时 : " + (System.currentTimeMillis() - startTime+"ms"));
        result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
        result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
        result.setData(user);
        return result ;
    }
    @RequestMapping("queryFields")
    @ResponseBody
    private Result queryFields(@RequestBody  JSONObject map){
        Result result = new Result();
         /*token登陆验证*/
        if(!mongoService.validateToken(map.get("token"))){
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getDesc());
            return  result;
        }
        Integer skip = map.get("skip") !=null ? Integer.valueOf(map.get("skip").toString()):0;
        Integer limit = map.get("limit") !=null ? Integer.valueOf(map.get("limit").toString()):0;
        /*查询操作*/
         /*查询操作*/
        long startTime = System.currentTimeMillis();
        List<Document> list = mongoService.queryFields(TableName.TEST_USER,map,null,skip != null ? skip:0 , limit != null ? limit:0);
        log.info("mongoJson/queryFields耗时 : " + (System.currentTimeMillis() - startTime+"ms"));
        result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
        result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
        result.setData(list);
        return result ;

    }

    @RequestMapping("queryCount")
    @ResponseBody
    private Result queryCount(@RequestBody JSONObject map){
        Result result = new Result();
         /*token登陆验证*/
        if(!mongoService.validateToken(map.get("token"))){
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getDesc());
            return  result;
        }
        long startTime = System.currentTimeMillis();
        long count =  mongoService.queryCount(TableName.TEST_USER,map);
        log.info("mongoJson/queryFields耗时 : " + (System.currentTimeMillis() - startTime+"ms"));
        result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
        result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
        result.setReturnCode(ValidateReturnCodeEnum.USER_QUERYCOUNT__SUCCESS.getCode());
        result.setReturnMsg(ValidateReturnCodeEnum.USER_QUERYCOUNT__SUCCESS.getDesc());
        result.setData(count);
        return result ;
    }
    @RequestMapping("delete")
    @ResponseBody
    private Result delete(@RequestBody JSONObject map){
        Result result = new Result();
         /*token登陆验证*/
        if(!mongoService.validateToken(map.get("token"))){
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getCode());
            result.setReturnMsg(ValidateReturnCodeEnum.USER_VALIDATE_LOGIN_FAILURE.getDesc());
            return  result;
        }
        mongoService.delete(TableName.TEST_USER,map);
        result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
        result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
        result.setReturnCode(ValidateReturnCodeEnum.USER_QUERYCOUNT__SUCCESS.getCode());
        result.setReturnMsg(ValidateReturnCodeEnum.USER_QUERYCOUNT__SUCCESS.getDesc());
        return result ;
    }
}
